package com.viettel.data;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.viettel.BO.CandidateBean;
import com.viettel.BO.RecruitmentAnouncementBean;

public class CandidateFetcher {

	private final String NAMESPACE = "http://webservice.viettel.com/";
	private final String METHOD_NAME = "getCandidateCareer";
	private final String SOAP_ACTION = "http://webservice.viettel.com/getCandidateCareer";
	private final String URL = "http://192.168.5.101:8084/RMSPortal/RMSWebService?wsdl";

	private SoapSerializationEnvelope envelope;
	private ArrayList<CandidateBean> listResult;

	public CandidateFetcher(Long portalUserId) {
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo quotesProperty = new PropertyInfo();
		quotesProperty.setName("portalUserId");
		quotesProperty.setValue(portalUserId);
		quotesProperty.setType(Long.class);
		request.addProperty(quotesProperty);

		envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.setOutputSoapObject(request);
	}

	public ArrayList<CandidateBean> getList() {
		HttpTransportSE httpRequest = new HttpTransportSE(URL);
		CandidateBean entity = new CandidateBean();
		listResult = new ArrayList<CandidateBean>();
		try {
			httpRequest.call(SOAP_ACTION, envelope);
			SoapObject response2 = (SoapObject) envelope.bodyIn;
			int iToltalCount = response2.getPropertyCount();
			if (iToltalCount > 0) {
				SoapObject element;
				for (int i = 0; i < iToltalCount; i++) {
					element = (SoapObject) response2.getProperty(i);
					entity = new CandidateBean();
					if (element.hasProperty("candidateId")
							&& element.getProperty("candidateId") != null) {
						entity.setCandidateId(Long.parseLong(element
								.getProperty("candidateId").toString()));
					}
					if (element.hasProperty("positionName")
							&& element.getProperty("positionName") != null) {
						entity.setPositionName(element.getProperty(
								"positionName").toString());
					}
					if (element.hasProperty("jobDescription")
							&& element.getProperty("jobDescription") != null) {
						entity.setJobDescription(element.getProperty(
								"jobDescription").toString());
					}
					if (element.hasProperty("applyDate")
							&& element.getProperty("applyDate") != null) {
						entity.setApplyDate(element.getProperty("applyDate")
								.toString());
					}
					if (element.hasProperty("status")
							&& element.getProperty("status") != null) {
						entity.setStatus(element.getProperty("status")
								.toString());
					}
					if (element.hasProperty("organizationName")
							&& element.getProperty("organizationName") != null) {
						entity.setOrganizationName(element.getProperty(
								"organizationName").toString());
					}
					listResult.add(entity);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listResult;
	}
}
